"""
This file is for communication between this API and iTOL servers
This also processes and stores information returned from the server
See the README.txt for details
@author: Albert Wang (albertyw@mit.edu)
@date: September 16, 2010
"""
import urllib2
import urllib2_file


class Comm:
    """
    This class handles communication between the API and the iTOL servers
    This also processes and stores information returned from the server
    """
    def __init__(self):
        """
        Initialize
        """
        self.uploadURL = 'http://itol.embl.de/batch_uploader.cgi'
        self.export_url = 'http://itol.embl.de/batch_downloader.cgi'
        self.upload_output = ''
        self.export_output = ''
        self.tree_id = ''
    
    ###Submitting the Tree for Processing
    def upload_tree(self, params):
        """
        Submit the File to Itol using api at self.upload_url
        @param params:
        @return: true if connection was established to server
        """
        #Send to iTol and read return
        url_handle = urllib2.urlopen(self.uploadURL, params)
        data = url_handle.read()
        url_handle.close()
        self.upload_output = data
        good_upload = self.parse_upload()
        return good_upload
    
    def parse_upload(self):
        """
        Parse the raw returned output for uploading to iTOL
        The output is read from self.upload_output
        @return: True if the tree is uploaded successfully or successfully with warnings; False if error occured
        """
        #Parse return statement and decide whether upload good
        if self.upload_output[0:7] == 'SUCCESS':   #Success
            self.tree_id = self.upload_output[9:len(self.upload_output)-1]
            good_upload = True
        else:
            good_upload = False
            if self.upload_output[0:3] != 'ERR':   #Success with Warnings
                self.tree_id = self.upload_output[\
                    self.upload_output.rfind('SUCCESS')+\
                    9:self.upload_output.rfind('SUCCESS')+33]
        return good_upload
    
    ###Export the Tree Image
    def export_image(self, params):
        """
        Submit an export request to Itol using api at self.export_url
        @return: true if connection was established to server
        """
        #Send to iTOL and read return
        url_handle = urllib2.urlopen(self.export_url, params)
        self.export_output = url_handle.read()
        url_handle.close()
        return self.export_output
    
